package com.cw.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.cw.annotation.AutoYard;
import com.cw.annotation.OperationLogAnno;
import com.cw.common.api.ResultObject;
import com.cw.common.constant.Response;
import com.cw.dto.ApplicationBedDTO;
import com.cw.entity.ApplicationBed;
import com.cw.request.BedAppicationDownLoad;
import com.cw.service.IApplicationBedService;
import com.cw.task.DaySurgeryTask;
import com.cw.websocketlocal.WebSocket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ly
 * @since 2021-12-13
 */
@Api(tags = {"申请弹窗表登记"})
@RestController
@RequestMapping("/application-bed")

public class ApplicationBedController {

    @Autowired
    private IApplicationBedService targetService;

    @Autowired
    WebSocket webSocket;

    @Autowired
    private DaySurgeryTask daySurgeryTask;

    @PostMapping("/UpdateApplication")
    @ApiOperation("修改")
    public ResultObject UpdateApplication(@RequestBody ApplicationBed entity) {
        if (entity.getPreid() == null) {
            return ResultObject.errorReturn("未获取到记录ID!");
        }
        Response result = targetService.UpdateApplication(entity);
        return ResultObject.resultReturn(result);
    }


    /** 手动更新update按钮  已处理传1
     *
     * @return
     */

    @PostMapping("/getButtonUpdate")
    @ApiOperation("按钮手动更新/已处理")
    public ResultObject getButtonUpdate(Integer preid,String isHandle){
        List<BigDecimal> lstPreid =new ArrayList<>();
        lstPreid.add(BigDecimal.valueOf(preid));
        return ResultObject.resultReturn(targetService.UpdateBatchApplicationStatus(lstPreid,null,isHandle));
    }


    @PostMapping("/getAppLiCation")
    @ApiOperation("查询")
    public ResultObject getISNotice(@RequestBody ApplicationBed entity) {
        return ResultObject.successReturn(targetService.getISNotice(entity));
    }

    /**
     * 床位预约查询
     * @param entity
     * @return
     */
    @AutoYard
    @PostMapping("/getBedReservationQuery")
    @ApiOperation("床位预约查询")
    public ResultObject getBedReservationQuery(@RequestBody ApplicationBed entity) {
        return ResultObject.successReturn(targetService.getBedReservationQuery(entity));
    }


    /**
     * 床位申请
     * @param entity
     * @return
     */
    @PostMapping("/addAppLiCation")
    @ApiOperation("新增")
    @OperationLogAnno(site = "集管床位申请（外接页面）", operation = "申请床位", bz = "医生申请",
            method = "/application-bed/addAppLiCation", type = "INSERT")
    public ResultObject addPopup(@RequestBody ApplicationBed entity) throws IOException {
        //查询是否已经申请过
        ApplicationBed selectEntity =new ApplicationBed();
        selectEntity.setPreid(entity.getPreid());
        selectEntity.setRootState("1");
        List<ApplicationBed> lstExtist =targetService.getInquire(selectEntity);
        if(!CollectionUtils.isEmpty(lstExtist))
        {
            return ResultObject.resultReturn(Response.error("该患者已申请床位,请勿重复申请"));
        }
        Response result = targetService.addPopup(entity);
        ApplicationBed searchInfo =new ApplicationBed();
        searchInfo.setIsHandle("0");
        searchInfo.setRootState("1");
        List<ApplicationBed> listNoHandle =targetService.getInquire(searchInfo);
        webSocket.sendMessageAll(String.valueOf(listNoHandle.size()),null);
        return ResultObject.resultReturn(result);
    }

    /**
     * 获取未处理人员数量用于启动时显示
     */
    @GetMapping("/getNoHandleListCount")
    @ApiOperation("获取未处理人员数量用于启动时显示")
    public void getNoHandleListCount() throws IOException {
    //消息提示数字
        ApplicationBed searchInfo =new ApplicationBed();
        //获取当前时间的前一天
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        searchInfo.setCreateTime(sdf2.format(date));
        //未处理的患者,就是有申请的患者
        searchInfo.setIsHandle("0");
        //这里过滤了假删除的数据，假删除“0”  真数据“1”
        searchInfo.setRootState("1");
        List<ApplicationBed> listNoHandle =targetService.getInquire(searchInfo);
        webSocket.sendMessageAll(String.valueOf(listNoHandle.size()),null);
    }

    @PostMapping("/Deleteinformation")
    @ApiOperation("删除")
    public ResultObject Deleteinformation(Integer preid) {
        if (preid == null || preid == 0){
            return ResultObject.errorReturn("请选择要删除的记录");
        }
        List<BigDecimal> listPerid =new ArrayList<>();
        listPerid.add(BigDecimal.valueOf(preid));
        Response result = targetService.Deleteinformation(listPerid);
        return ResultObject.resultReturn(result);
    }

    /**
     * 修改显示状态  1为显示  0为未显示
     * @param
     * @param
     * @return
     */
    @PostMapping("/updateRootState")
    @ApiOperation("编辑按钮，多选，假删除")
    @OperationLogAnno(site = "住院预约记录", operation = "床位申请-->删除患者", bz = "门诊办人员操作",
            method = "/application-bed/updateRootState", type = "DELETE")
    public ResultObject updateRootState(@RequestBody List<String> Listpreid){
        if (Listpreid==null){
            return ResultObject.errorReturn("请选择要删除的记录");
        }
        return ResultObject.resultReturn(targetService.deleteByPreid(Listpreid));
    }



    /**
     * @author xnd
     * @since 2021-10-28
     * 床位申请打印下载
     */
    @ApiOperation("床位申请--excel下载")
    @PostMapping("/getBedApplicationDownLoadExcel")
    public void getBedApplicationDownLoadExcel(@RequestBody BedAppicationDownLoad requestInfo) throws IOException {
        targetService.geBedApplicationDownLoadExcel(requestInfo);
    }

    @ApiOperation("发送通知")
    @PostMapping("/sendMessageToDoctor")
    public ResultObject sendMessageToDoctor(@RequestBody ApplicationBedDTO dto){
        return ResultObject.resultReturn(targetService.sendMessageToDoctor(dto));
    }

    @ApiOperation("日间手术通知")
    @PostMapping("/daySurgery")
    public void daySurgery(){
         daySurgeryTask.daySurgery();
    }

}

